package com.aote.model.block;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLTableModel;
import com.browsesoft.tools.BSCalendar;

/**
 * 日期天数块model
 * 
 */
public class MonthDaysBlockModel implements HTMLTableModel {
	/**
	 * 组件
	 */
	protected HTMLBasicComponent component = null;

	/**
	 * 开始日期
	 */
	private BSCalendar startCalendar = BSCalendar.now();

	/**
	 * 结束日期
	 */
	private BSCalendar endCalendar = BSCalendar.now();

	private String dateFormat = "";

	public void init(HttpServletRequest request) throws Exception {
		this.dateFormat = this.component.getElement().getAttribute("format");
		String id = request.getParameter("id");
		if (id != null && !id.equals("")) {
			// 设置日期
			setDateFormEntity(id);
		}

	}

	private String getDateFormat() {
		return this.dateFormat;
	}

	private void setDateFormEntity(String id) {
		Entity entity = (Entity) EntityManager.getInstance().getObject(id);
		// 得到开始日期
		String startAttrName = this.component.getElement().getAttribute(
				"parentStartDate");
		String endAttrName = this.component.getElement().getAttribute(
				"parentEndDate");
		String startDate = (String) entity.getAttributes().get(startAttrName);
		String endDate = (String) entity.getAttributes().get(endAttrName);
		// 得到日期属性值和格式
		String format = getDateFormat();
		setDateRewrite(this.startCalendar, startDate, format);
		setDateRewrite(this.endCalendar, endDate, format);

	}

	private void setDateRewrite(BSCalendar calendar, String date, String format) {
		// 得到年月
		int year = getYear(date, format);
		int month = getMonth(date, format);
		int day = getDay(date, format);
		calendar.setYear(year);
		calendar.setMonth(month);
		calendar.setDay(day);
	}

	/**
	 * 设置初始日期
	 */
	private void setDate(String startDate, String endDate) {
		String format = getDateFormat();
		setDateRewrite(this.startCalendar, startDate, format);
		setDateRewrite(this.endCalendar, endDate, format);
	}

	private int getYear(String date, String format) {
		int index = format.indexOf("yyyy");
		String result = date.substring(index, index + 4);
		return Integer.parseInt(result);
	}

	private int getMonth(String date, String format) {
		int index = format.indexOf("mm");
		String result = date.substring(index, index + 2);
		return Integer.parseInt(result);
	}

	private int getDay(String date, String format) {
		int index = format.indexOf("dd");
		String result = date.substring(index, index + 2);
		return Integer.parseInt(result);
	}

	public void setComponent(HTMLBasicComponent component) {
		this.component = component;
	}

	public int getColumnCount() {
		String startDate = this.startCalendar.toString(this.getDateFormat());
		String endDate = this.endCalendar.toString(this.getDateFormat());
		if (startDate.equals(endDate)) {
			return 0;
		}
		return this.endCalendar.getDayDispersion(this.startCalendar) + 1;
	}

	public String getColumnName(int col) {
		BSCalendar c = this.startCalendar.getCalendarAfterDay(col);
		return c.toString(this.getDateFormat());
	}

	public int getRowCount() {
		return 0;
	}

	public void setCondition(String condition) {
	}

	public void modifyCellValue(Element td, Document doc, int row, int col) {
	}

	public String getType(int col) {
		return null;
	}

	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String action = request.getParameter("action");
		if (action != null && action.equals("recreate")) {
			String startDate = request.getParameter("statmonth");
			String endDate = request.getParameter("endmonth");
			setDate(startDate, endDate);
		}
	}

}
